package com.ny.dao.impl;

import com.ny.dao.ManagerDao;
import com.ny.entity.Manager;
import com.ny.until.DbUntil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ManagerDaoImpl implements ManagerDao {
    private DbUntil  dbUntil = new DbUntil();

    @Override
    public Manager login(String name, String password) throws Exception {
        Connection  connection = dbUntil.getCon();

        String sql = "select * from managers where name = ? and password = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, name);
        preparedStatement.setString(2, password);
        ResultSet resultSet = preparedStatement.executeQuery();

        Manager manager = new Manager();
        if (resultSet.next()){
            manager.setManagerId(resultSet.getInt("manager_id"));
            manager.setName(resultSet.getString("name"));
            manager.setPhone(resultSet.getString("phone"));
            manager.setPassword(resultSet.getString("password"));
            return manager;
        }
        return null;
    }

    @Override
    public List<Manager> findAllManager() throws SQLException, ClassNotFoundException {
        Connection connection = dbUntil.getCon();
        String sql = "SELECT * FROM managers";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();

        List<Manager> managerList = new ArrayList<Manager>();

        while (resultSet.next()){
            Manager manager = new Manager();
            manager.setManagerId(resultSet.getInt("manager_id"));
            manager.setName(resultSet.getString("name"));
            manager.setPhone(resultSet.getString("phone"));
            manager.setPassword(resultSet.getString("password"));

            managerList.add(manager);
        }
        return managerList;
    }

    @Override
    public int insertManager(Manager manager) throws SQLException, ClassNotFoundException {
        Connection connection = dbUntil.getCon();
        String sql = "INSERT INTO managers(name, phone, password) VALUES(?, ?, ?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, manager.getName());
        preparedStatement.setString(2, manager.getPhone());
        preparedStatement.setString(3, manager.getPassword());

        return preparedStatement.executeUpdate();
    }

    @Override
    public int updateManager(Manager manager) throws SQLException, ClassNotFoundException {
        Connection connection = dbUntil.getCon();
        String sql = "UPDATE managers SET name = ?, phone = ?, password = ? WHERE manager_id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, manager.getName());
        preparedStatement.setString(2, manager.getPhone());
        preparedStatement.setString(3, manager.getPassword());
        preparedStatement.setInt(4, manager.getManagerId());

        return preparedStatement.executeUpdate();
    }

    @Override
    public int deleteManager(String name) throws SQLException, ClassNotFoundException {
        Connection  connection = dbUntil.getCon();
        String sql = "DELETE FROM managers WHERE name = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, name);

        return preparedStatement.executeUpdate();
    }


}
